package com.jiangyifen.vmw.sdk;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jiangyifen.vmw.sdk.command.bean.AccessNumber;
import com.jiangyifen.vmw.sdk.command.bean.AccessNumberRoute;
import com.jiangyifen.vmw.sdk.command.bean.Agent;
import com.jiangyifen.vmw.sdk.command.bean.Blacklist;
import com.jiangyifen.vmw.sdk.command.bean.Gateway;
import com.jiangyifen.vmw.sdk.command.bean.Queue;
import com.jiangyifen.vmw.sdk.command.bean.SipPhone;
import com.jiangyifen.vmw.sdk.command.bean.Tenant;
import com.jiangyifen.vmw.sdk.command.internal.SimpleResponse;
import com.jiangyifen.vmw.sdk.command.response.AccessNumberGetAllResponse;
import com.jiangyifen.vmw.sdk.command.response.AccessNumberRouteGetAllResponse;
import com.jiangyifen.vmw.sdk.command.response.AgentGetAllResponse;
import com.jiangyifen.vmw.sdk.command.response.AgentSipPhoneBindInfoResponse;
import com.jiangyifen.vmw.sdk.command.response.BlacklistGetAllResponse;
import com.jiangyifen.vmw.sdk.command.response.GatewayGetAllResponse;
import com.jiangyifen.vmw.sdk.command.response.GetCdrByCallUuidResponse;
import com.jiangyifen.vmw.sdk.command.response.GetChannelCountResponse;
import com.jiangyifen.vmw.sdk.command.response.GetChannelResponse;
import com.jiangyifen.vmw.sdk.command.response.GetConferenceStatusResponse;
import com.jiangyifen.vmw.sdk.command.response.GetGatewayStatusResponse;
import com.jiangyifen.vmw.sdk.command.response.GetMobileInfoResponse;
import com.jiangyifen.vmw.sdk.command.response.GetQueueStatusResponse;
import com.jiangyifen.vmw.sdk.command.response.GetSipPhoneStatusResponse;
import com.jiangyifen.vmw.sdk.command.response.IsExtenResponse;
import com.jiangyifen.vmw.sdk.command.response.IsPhoneNumberBlockInResponse;
import com.jiangyifen.vmw.sdk.command.response.IsPhoneNumberBlockOutResponse;
import com.jiangyifen.vmw.sdk.command.response.OriginateResponse;
import com.jiangyifen.vmw.sdk.command.response.QueueGetAllResponse;
import com.jiangyifen.vmw.sdk.command.response.QueueMemberPauseResponse;
import com.jiangyifen.vmw.sdk.command.response.SipPhoneGetAllResponse;
import com.jiangyifen.vmw.sdk.command.response.TenantActionNodeTreeGetResponse;
import com.jiangyifen.vmw.sdk.command.response.TenantGetAllResponse;
import com.jiangyifen.vmw.sdk.command.response.TtsResponse;

/**
 * @version 2.0
 */
public class SampleCode {

	/**
	 * 注意！！！！！！！！
	 * 
	 * rsmwUrl 格式请参考 VMW 2 API 文档
	 */
	private static String rsmwUrl = "http://192.168.8.227:18080/vmw/api/2.0";

	/**
	 * 注意！！！！！！！！
	 * 
	 * apiSecret在rsmw系统的API界面中配置。 即：启用apiSecret后的apiSecret
	 */
	private static String apiSecret = "pA55w0rd";

	private static int connectTimeout = 2000;
	private static int readTimeout = 2000;

	private static VmwServer rsmwServer = new VmwServer(rsmwUrl, apiSecret, connectTimeout, readTimeout);

	
	public static void main(String[] args) {

	    // //tts语音合成
        Long tenantId = 0L;
        String content = "tts语音合成";
        TtsResponse ttsResponse = rsmwServer.tts(tenantId,content);
        System.out.println(ttsResponse.toJsonString());

	}

	public static void main1(String[] args) {

		// 这里调用了所有的接口方法，您在测试某个接口时，请先注释掉其他的调用，以免影响您查看测试结果

		// 5.1 【新增Agent】
		Agent agent = new Agent();
		agent.setPwd("123");
		agent.setAgentName("Zhang san");
		agent.setAid("uuid");
		agent.setJobNumber(1001L);
		agent.setTenantId(0L);
		SimpleResponse agentSaveResponse = rsmwServer.agentSave(agent);
		System.out.println(agentSaveResponse.toJsonString());

		// 5.1 【删除Agent】
		List<String> agentAidList = new ArrayList<>();
		agentAidList.add("1055");
		agentAidList.add("1054");
		agentAidList.add("1053");
		agentAidList.add("1052");
		agentAidList.add("1051");
		agentAidList.add("1050");
		SimpleResponse agentDeleteResponse = rsmwServer.agentDelete(agentAidList);
		System.out.println(agentDeleteResponse.toJsonString());

		// 5.1 【同步Agent】
		List<Agent> agentList = new ArrayList<>();
		Agent a1 = new Agent();
		a1.setAgentName("张三");
		a1.setAid("1001");
		a1.setJobNumber(1001L);

		Agent a2 = new Agent();
		a2.setAgentName("李四");
		a2.setAid("1002");
		a2.setJobNumber(1002L);

		agentList.add(a1);
		agentList.add(a2);
		SimpleResponse agentSyncResponse = rsmwServer.agentSync(0L, agentList);
		System.out.println(agentSyncResponse.toJsonString());

		// 5.1 【获取所有Agent】
		AgentGetAllResponse agentListAllResponse = rsmwServer.agentGetAll(0L);
		System.out.println(agentListAllResponse.toJsonString());

		// 5.2 【绑定】
		SimpleResponse agentSipPhoneBindResponse = rsmwServer.bind("9999", "8001");
		System.out.println(agentSipPhoneBindResponse.toJsonString());

		// 5.2 【解绑】 根据座席解绑分机
		SimpleResponse agentSipPhoneUnBindResponse1 = rsmwServer.unBind("9999", null);
		System.out.println(agentSipPhoneUnBindResponse1.toJsonString());

		// 5.2 【解绑】 根据分机解绑座席
		SimpleResponse agentSipPhoneUnBindResponse2 = rsmwServer.unBind(null, "8001");
		System.out.println(agentSipPhoneUnBindResponse2.toJsonString());

		// 5.3 【绑定关系查询】 查询座席绑定的分机
		AgentSipPhoneBindInfoResponse agentSipPhoneBindInfoResponse1 = rsmwServer.getBindInfo("9999", null);
		System.out.println(agentSipPhoneBindInfoResponse1.toJsonString());

		// 5.3 【绑定关系查询】查询分机绑定的座席
		AgentSipPhoneBindInfoResponse agentSipPhoneBindInfoResponse2 = rsmwServer.getBindInfo(null, "8001");
		System.out.println(agentSipPhoneBindInfoResponse2.toJsonString());

		// 5.4 【置忙】
		QueueMemberPauseResponse qMemberPauseResponse = rsmwServer.queueMemberPause("SIP/8001", "置忙原因：你猜");
		System.out.println(qMemberPauseResponse.toJsonString());

		// 5.4 【置闲】
		QueueMemberPauseResponse qMemberUnPauseResponse = rsmwServer.queueMemberUnPause("SIP/8001");
		System.out.println(qMemberUnPauseResponse.toJsonString());

		// 5.5 【实时并发数查询】
		GetChannelCountResponse getChannelCountResponse = rsmwServer.getChannelCount();
		System.out.println(getChannelCountResponse.toJsonString());

		// 5.6 【发起呼叫】
		OriginateResponse originateResponse = rsmwServer.originate(0L, 0, "8001", "", "", "", "", "", "8002", "", "",
				"", "userDataXXXXX");
		System.out.println(originateResponse.toJsonString());

		// 5.7 【获取指定SipPhone的当前Channel】
		GetChannelResponse getChannelResponse = rsmwServer.getChannel("8001");
		System.out.println(getChannelResponse.toJsonString());

		// 5.8 【呼叫保持、恢复】
		SimpleResponse holdResponse = rsmwServer.hold("8001");
		System.out.println(holdResponse.toJsonString());

		SimpleResponse unholdResponse = rsmwServer.unhold("8001");
		System.out.println(unholdResponse.toJsonString());

		// 5.9 【桥接channel】
		SimpleResponse bridgeResponse = rsmwServer.bridge("SIP/8001-XXXXXXXX", "SIP/8002-XXXXXXXX");
		System.out.println(bridgeResponse.toJsonString());

		// 5.10 【转接Channel】
		SimpleResponse redirectResponse = rsmwServer.redirect("SIP/8001-XXXXXXXX", "_moh", "13391026171",
				"SIP/8002-XXXXXXXX", "core_route", "13391026172");
		System.out.println(redirectResponse.toJsonString());

		// 5.11 【挂断Channel】
		SimpleResponse hangupResponse = rsmwServer.hangup("SIP/8001-XXXXXXXX");
		System.out.println(hangupResponse.toJsonString());

		// 5.12 【获取所有SipPhone状态】
		GetSipPhoneStatusResponse getSipPhoneStatusResponse = rsmwServer.getSipPhoneStatus();
		System.out.println(getSipPhoneStatusResponse.toJsonString());

		// 5.12 【获取所有SipPhone状态】
		GetGatewayStatusResponse getGatewayStatusResponse = rsmwServer.getGatewayStatus();
		System.out.println(getGatewayStatusResponse.toJsonString());

		// 5.13 【查询归属地】
		GetMobileInfoResponse getMobileInfoResponse = rsmwServer.getMobileInfo("13391026171");
		System.out.println(getMobileInfoResponse.toJsonString());

		// 5.14 【查询cdr】
		GetCdrByCallUuidResponse response1 = rsmwServer.getCdrByCallUuid("f97ea90287244c87b2d6d4b60da61d16");
		System.out.println(response1.toJsonString());
		GetCdrByCallUuidResponse response2 = rsmwServer.getCdrByCallUuid("1493866989.59");
		System.out.println(response2.toJsonString());

		// 5.15【服务质量评价】
		SimpleResponse sqeResponse = rsmwServer.sqe("8001", "http://ip:port/question.wav",
				"http://ip:port/thankyou.wav");
		System.out.println(sqeResponse.toJsonString());

		// 5.16 【判断某个号码是否内部exte】
		IsExtenResponse isExtenResponse = rsmwServer.isExten("8001");
		System.out.println(isExtenResponse.toJsonString());

		GetQueueStatusResponse getQueueStatusResponse = rsmwServer.getQueueStatus();
		System.out.println(getQueueStatusResponse.toJsonString());

		// 获取租户权限树
		TenantActionNodeTreeGetResponse tenantActionNodeTreeGetResopnse = rsmwServer.tenantActionNodeTreeGet();
		System.out.println(tenantActionNodeTreeGetResopnse.toJsonString());

		// tenant
		Tenant tenant = new Tenant();
		tenant.setName("tenant1");
		// tenant.setTenantPrefix("1001");
		tenant.setExpireDate(new Date());
		SimpleResponse tenantSaveResopnse = rsmwServer.tenantSave(tenant);
		System.out.println(tenantSaveResopnse.toJsonString());

		tenant.setId(tenantSaveResopnse.getId());
		tenant.setName("tenant2");
		SimpleResponse tenantUpdateResopnse = rsmwServer.tenantUpdate(tenant);
		System.out.println(tenantUpdateResopnse.toJsonString());

		TenantGetAllResponse tenantGetAllResopnse = rsmwServer.tenantGetAll();
		System.out.println(tenantGetAllResopnse.toJsonString());

		List<Long> tenantIdList = new ArrayList<>();
		tenantIdList.add(tenant.getId());
		SimpleResponse tenantDeleteResopnse = rsmwServer.tenantDelete(tenantIdList);
		System.out.println(tenantDeleteResopnse.toJsonString());

		// accessNumber
		AccessNumber accessNumber = new AccessNumber();
		accessNumber.setTenantId(0L);
		accessNumber.setNumber("1111");
		SimpleResponse accessNumberSaveResopnse = rsmwServer.accessNumberSave(accessNumber);
		System.out.println(accessNumberSaveResopnse.toJsonString());

		accessNumber.setId(accessNumberSaveResopnse.getId());
		accessNumber.setNumber("22222");
		SimpleResponse accessNumberUpdateResopnse = rsmwServer.accessNumberUpdate(accessNumber);
		System.out.println(accessNumberUpdateResopnse.toJsonString());

		AccessNumberGetAllResponse accessNumberGetAllResopnse = rsmwServer.accessNumberGetAll();
		System.out.println(accessNumberGetAllResopnse.toJsonString());

		List<Long> accessNumberIdList = new ArrayList<>();
		accessNumberIdList.add(accessNumber.getId());
		SimpleResponse accessNumberDeleteResopnse = rsmwServer.accessNumberDelete(accessNumberIdList);
		System.out.println(accessNumberDeleteResopnse.toJsonString());

		// gateway
		Gateway g = new Gateway();
		g.setTenantId(0L);
		g.setName("gw0");
		g.setMaxChannel(10);
		g.setMode("REG_NO");
		SimpleResponse gatewaySaveResponse = rsmwServer.gatewaySave(g);
		System.out.println(gatewaySaveResponse.toJsonString());

		g.setId(gatewaySaveResponse.getId());
		g.setName("gw00");
		SimpleResponse gatewayUpdateResopnse = rsmwServer.gatewayUpdate(g);
		System.out.println(gatewayUpdateResopnse.toJsonString());

		GatewayGetAllResponse gatewayGetAllResopnse = rsmwServer.gatewayGetAll();
		System.out.println(gatewayGetAllResopnse.toJsonString());

		List<Long> gatewayIdList = new ArrayList<>();
		gatewayIdList.add(g.getId());
		SimpleResponse gatewayDeleteResopnse = rsmwServer.gatewayDelete(gatewayIdList);
		System.out.println(gatewayDeleteResopnse.toJsonString());

		// accessNumber gateway link
		Long accessNumberId = 10L;
		List<Long> linkGatewayIdList = new ArrayList<>();
		linkGatewayIdList.add(5L);
		SimpleResponse accessNumberGatewayLinkResponse = rsmwServer.accessNumberGatewayLink(accessNumberId,
				linkGatewayIdList);
		System.out.println(accessNumberGatewayLinkResponse.toJsonString());

		// sipphone
		SipPhone sipPhone = new SipPhone();
		sipPhone.setTenantId(0L);
		sipPhone.setName("8001");
		sipPhone.setTimeout(60);
		sipPhone.setCallForwardType("NONE");
		SimpleResponse sipPhoneSaveResponse = rsmwServer.sipPhoneSave(sipPhone);
		System.out.println(sipPhoneSaveResponse.toJsonString());

		sipPhone.setId(sipPhoneSaveResponse.getId());
		sipPhone.setName("8002");
		SimpleResponse sipPhoneUpdateResopnse = rsmwServer.sipPhoneUpdate(sipPhone);
		System.out.println(sipPhoneUpdateResopnse.toJsonString());

		SipPhoneGetAllResponse sipPhoneGetAllResopnse = rsmwServer.sipPhoneGetAll();
		System.out.println(sipPhoneGetAllResopnse.toJsonString());

		List<Long> sipPhoneIdList = new ArrayList<>();
		sipPhoneIdList.add(sipPhone.getId());
		SimpleResponse sipPhoneDeleteResopnse = rsmwServer.sipPhoneDelete(sipPhoneIdList);
		System.out.println(sipPhoneDeleteResopnse.toJsonString());

		// accessNumberRoute
		AccessNumberRoute accessNumberRoute = new AccessNumberRoute();
		accessNumberRoute.setTenantId(0L);
		accessNumberRoute.setName("1111");
		accessNumberRoute.setSeq(1L);
		accessNumberRoute.setStartDate(new Date());
		accessNumberRoute.setEndDate(new Date());
		accessNumberRoute.setAccessNumber("6666");
		accessNumberRoute.setDestType("SIPPHONE");
		accessNumberRoute.setDest("8001");
		SimpleResponse accessNumberRouteSaveResponse = rsmwServer.accessNumberRouteSave(accessNumberRoute);
		System.out.println(accessNumberRouteSaveResponse.toJsonString());

		accessNumberRoute.setId(accessNumberRouteSaveResponse.getId());
		accessNumberRoute.setName("2222");
		SimpleResponse accessNumberRouteUpdateResopnse = rsmwServer.accessNumberRouteUpdate(accessNumberRoute);
		System.out.println(accessNumberRouteUpdateResopnse.toJsonString());

		AccessNumberRouteGetAllResponse accessNumberRouteGetAllResopnse = rsmwServer.accessNumberRouteGetAll();
		System.out.println(accessNumberRouteGetAllResopnse.toJsonString());

		List<Long> accessNumberRouteIdList = new ArrayList<>();
		accessNumberRouteIdList.add(accessNumberRoute.getId());
		SimpleResponse accessNumberRouteDeleteResopnse = rsmwServer.accessNumberRouteDelete(accessNumberRouteIdList);
		System.out.println(accessNumberRouteDeleteResopnse.toJsonString());

		// queue
		Queue queue = new Queue();
		queue.setTenantId(0L);
		queue.setName("1111");

		SimpleResponse queueSaveResponse = rsmwServer.queueSave(queue);
		System.out.println(queueSaveResponse.toJsonString());

		queue.setId(queueSaveResponse.getId());
		queue.setName("2222");
		SimpleResponse queueUpdateResopnse = rsmwServer.queueUpdate(queue);
		System.out.println(queueUpdateResopnse.toJsonString());

		QueueGetAllResponse queueGetAllResopnse = rsmwServer.queueGetAll();
		System.out.println(queueGetAllResopnse.toJsonString());

		List<Long> queueIdList = new ArrayList<>();
		queueIdList.add(queue.getId());
		SimpleResponse queueDeleteResopnse = rsmwServer.queueDelete(queueIdList);
		System.out.println(queueDeleteResopnse.toJsonString());

		// queue member
		SimpleResponse queueMemberSaveOutboundNumberResponse = rsmwServer.queueMemberSaveOutboundNumber(5L,
				"13391026171", "gw1", 5);
		System.out.println(queueMemberSaveOutboundNumberResponse.toJsonString());

		SimpleResponse queueMemberDeleteOutboundNumberResponse = rsmwServer.queueMemberDeleteOutboundNumber(5L,
				queueMemberSaveOutboundNumberResponse.getId());
		System.out.println(queueMemberDeleteOutboundNumberResponse.toJsonString());

		List<Long> queueMemberSipPhoneIdList = new ArrayList<>();
		List<String> queueMemberAgentAidList = new ArrayList<>();
		SimpleResponse queueMemberSetSipPhoneOrAgentResponse = rsmwServer.queueMemberSetSipPhoneOrAgent(5L,
				queueMemberSipPhoneIdList, queueMemberAgentAidList);
		System.out.println(queueMemberSetSipPhoneOrAgentResponse.toJsonString());

		// 黑名单
		Blacklist b = new Blacklist();
		b.setPhonenumber("15755381763");
		b.setTenantId(1L);
		b.setTypee("BLOCK_OUT");// BLOCK_OUT[禁止呼出] BLOCK_IN[禁止呼入]
								// BLOCK_BOTH[禁止呼入呼出]
		b.setDescription("禁止呼出");

		// 保存黑名单
		SimpleResponse blacklistSaveResponse = rsmwServer.blacklistSave(b);
		System.out.println(blacklistSaveResponse.toJsonString());

		// 获取所有的黑名单
		BlacklistGetAllResponse blacklistGetAllResponse = rsmwServer.blacklistGetAll();
		System.out.println(blacklistGetAllResponse.toJsonString());

		// 黑名单呼入查询
		IsPhoneNumberBlockInResponse IsPhoneNumberBlockInResponse = rsmwServer.isPhoneNumberBlockIn(1L, "15755381763");
		System.out.println(IsPhoneNumberBlockInResponse.toJsonString());

		// 黑名单呼出查询
		IsPhoneNumberBlockOutResponse isPhoneNumberBlockOutResponse = rsmwServer.isPhoneNumberBlockOut(1L,
				"15755381761");
		System.out.println(isPhoneNumberBlockOutResponse.toJsonString());

		// 删除黑名单
		List<Long> idList = new ArrayList<>();
		idList.add(1L);
		idList.add(2L);
		SimpleResponse blacklistDeleteResponse = rsmwServer.blacklistDelete(idList);
		System.out.println(blacklistDeleteResponse.toJsonString());

		// 查询会议的状态
		String exten = "10009001";
		GetConferenceStatusResponse response = rsmwServer.getConferenceStatus(exten);
		System.out.println(response.toJsonString());

		// 邀请
		String extenInvite = "10009001";
		List<String> phoneNumberListInvite = new ArrayList<>();
		phoneNumberListInvite.add("10008001");
		phoneNumberListInvite.add("10008004");
		String gatewayInvite = "1111";
		String accessNumberInvite = "2222";
		SimpleResponse response11 = rsmwServer.conferenceInvite(extenInvite, gatewayInvite, accessNumberInvite, phoneNumberListInvite);
		System.out.println(response11.toJsonString());

		// 重新邀请
		String extenReInvite = "10009001";
		String phoneNumberReInvite = "10008001";
		SimpleResponse responseReInvite = rsmwServer.conferenceReInvite(extenReInvite, phoneNumberReInvite);
		System.out.println(responseReInvite.toJsonString());

		// 踢出
		String extenKick = "10009001";
		String channelKick = "SIP/10008001-00000008";
		SimpleResponse responseKick = rsmwServer.conferenceKick(extenKick, channelKick);
		System.out.println(responseKick.toJsonString());

		// //静音
		String extenMute = "10009001";
		String channelMute = "SIP/10008001-0000000c";
		SimpleResponse responseMute = rsmwServer.conferenceMute(extenMute, channelMute);
		System.out.println(responseMute.toJsonString());

		// //解除静音
		String extenUnMute = "10009001";
		String channelUnMute = "SIP/10008001-0000000c";
		SimpleResponse responseUnMute = rsmwServer.conferenceUnMute(extenUnMute, channelUnMute);
		System.out.println(responseUnMute.toJsonString());
		
		// //会议室锁定
		String extenLock = "10009001";
		SimpleResponse responseLock = rsmwServer.conferenceLock(extenLock);
		System.out.println(responseLock.toJsonString());
		
		// //会议室解锁
		String extenUnlock = "10009001";
		SimpleResponse responseUnlock = rsmwServer.conferenceUnlock(extenUnlock);
		System.out.println(responseUnlock.toJsonString());

		// //tts语音合成
		Long tenantId = 0L;
        String content = "tts语音合成";
        TtsResponse ttsResponse = rsmwServer.tts(tenantId,content);
        System.out.println(ttsResponse.toJsonString());
		
	}

}
